//
//  TRBaiduMapUtils.m
//  TLBS
//
//  Created by tarena on 14-4-2.
//  Copyright (c) 2014年 tarena. All rights reserved.
//

#import "TRBaiduMapUtils.h"
#import "SBJSON.h"
@implementation TRBaiduMapUtils
+(void)createPoiWithLocation:(CLLocationCoordinate2D)location andWeiboID:(NSString *)weiboID{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //去掉智能转换
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    NSDictionary *parameters = @{@"ak": @"tByH8ctdq7532n2xXFspe53j"
                                 ,@"geotable_id": @"46576"
                                 ,@"tags": @"TLBS"
                                 ,@"latitude": [NSString stringWithFormat:@"%f",location.latitude]
                                 ,@"longitude": [NSString stringWithFormat:@"%f",location.longitude]
                                 ,@"coord_type": @"3"
                                 ,@"weibo_id": weiboID
                                 ,@"title":@"abc"};
    [manager POST:@"http://api.map.baidu.com/geodata/v3/poi/create" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"result = %@",result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络出错：%@",[error localizedDescription]);
    }];

}

+(void)serchPoiByLocation:(NSString *)location callback:(void (^)(NSArray *))callback{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //去掉智能转换
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    
    CLLocationCoordinate2D l;
    l.latitude =39.904965;
    l.longitude =116.406636;

    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    int radius = [ud integerForKey:@"Radius"];
    
    NSDictionary *parameters = @{@"ak": @"tByH8ctdq7532n2xXFspe53j",@"":@"q"                         ,@"geotable_id": @"46576"
                                 ,@"location":[NSString stringWithFormat:@"%f,%f",l.longitude,l.latitude]
                                 ,@"radius": [NSString stringWithFormat:@"%d",radius]
                                 ,@"coord_type": @"3"
                                 ,@"tags": @"TLBS"
                                 ,@"page_index": @"0"
                                 ,@"page_size": @"50"
                                 };
    
    [manager GET:@"http://api.map.baidu.com/geosearch/v3/nearby" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *resultDic = [result JSONValue];
        NSArray *poiDics= [resultDic objectForKey:@"contents"];
        NSMutableArray *weiboIDs = [NSMutableArray array];
        for (NSDictionary *poiDic in poiDics) {
            NSString *weiboID = [poiDic objectForKey:@"weibo_id"];
            [weiboIDs addObject:weiboID];
        }
        NSLog(@"%@",weiboIDs);
        callback(weiboIDs);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络出错：%@",[error localizedDescription]);
    }];
}
@end
